package com.mol.market.ui_v4;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.network.ApiResponse;
import com.mol.market.common.network.MarketAPI;
import com.mol.market.common.util.ScreenRules;
import com.mol.market.common.util.ScreenRules.Rules;
import com.mol.market.common.util.Utils;
import com.mol.market.ui.core.GridAdapter;
import com.mol.market.ui.core.LazyLoadAdapter;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Author: Andrew.Wang
 * Date  : 13-1-28
 */
public class AppGeniusFragment extends SimpleLazyLoadFragmentLoader{

    @Override
    public Loader<ApiResponse> onCreateLoader(int id, Bundle args) {
        if (id == MarketAPI.ACTION_RECOMMEND_BY_USER) {
            return MarketAPI.initRecommendByUserLoader(getActivity(), getSize(), getStart());
        }
        return null;
    }

    @Override
    protected ArrayList<HashMap<String, Object>> getDataFromResult(int method, Object obj) {
        return (ArrayList<HashMap<String, Object>>)
                ((HashMap<String, Object>) obj).get(Constants.KEY_PRODUCT_LIST);
    }

    @Override
    protected void load() {
        getLoaderManager().restartLoader(
                MarketAPI.ACTION_RECOMMEND_BY_USER, null, this);
    }

    @Override
    protected LazyLoadAdapter initLazyLoadAdapter() {
        GridAdapter adapter = new GridAdapter(
                this,
                null,
                ScreenRules.getLayout(getWidthMatching(), Rules.Download),
                new String[]{
                        Constants.KEY_PRODUCT_ICON_URL,
                        Constants.KEY_PRODUCT_NAME,
                        Constants.KEY_PRODUCT_AUTHOR,
                        Constants.KEY_PRODUCT_IS_STAR,
                        Constants.KEY_PRODUCT_DOWNLOAD_COUNT,
                        Constants.KEY_PRODUCT_DOWNLOAD,
                        Constants.KEY_PRODUCT_SIZE,
                },
                new int[]{
                        R.id.iv_logo,
                        R.id.tv_name,
                        R.id.tv_description,
                        R.id.iv_star,
                        R.id.tv_downloading,
                        R.id.tv_download,
                        R.id.tv_size,
                });
        adapter.setProductList();
        adapter.setNumColumns(ScreenRules.getNumColumns(getWidthMatching(), Rules.Download));

        adapter.setOnItemClickListener(new GridAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int pos, int realPos, HashMap<String, Object> item) {
                String pid = (String) item.get(Constants.KEY_PRODUCT_ID);
                String name = (String) item.get(Constants.KEY_PRODUCT_NAME);
                Intent detailIntent = new Intent(getActivity(), ProductDetailActivity.class);
                detailIntent.putExtra(Constants.EXTRA_PRODUCT_ID, pid);
                detailIntent.putExtra(Constants.EXTRA_SOURCE_TYPE, Constants.SOURCE_TYPE_GFAN);
                detailIntent.putExtra(Constants.EXTRA_PRODUCT_NAME, name);
                getActivity().startActivity(detailIntent);
                Utils.trackEvent(getApplicationContext(), Constants.GROUP_2, "猜你喜欢-进入详情页");
            }
        });
        return adapter;
    }
}
